<?php

namespace App\Console\Commands;

use App\Models\org\OrganizationalStructure;
use App\Models\Student;
use App\Models\Teacher;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class getOrganizational extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'get:organizational';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try{
        for ($level = 1; $level < 10; $level++) {
            for ($page = 0; $page < 999; $page++) {
                $result = \College::getOrganizational($level, $page);
                if ($result) {
                    foreach ($result as $item) {
                        $org = OrganizationalStructure::query()->where(['key' => $item->id])->first();
                        if (!$org) {
                            $org = new OrganizationalStructure();
                            $org->key = $item->id;
                            $org->parent_key = $item->pid;
                            $org->jcbid = $item->jcbid;
                            $org->organizational_name = $item->name;
                            $org->organizational_level = $item->level;
                            $org->save();
                        }
                        if($org->organizational_level == 1){
                            $org->level_tab = '-'.$org->id.'-';
                        }else{
                            $parent = DB::table('organizational_structures')->where(['key' =>$org->parent_key])->first();
                            if($parent){
                                $org->level_tab = $parent->level_tab.$org->id.'-';
                            }else{
                                continue;
                            }
                        }
                        $org->save();
                    }
                } else {
                    break;
                }
            }
        }
        $orgs = DB::table('organizational_structures')->get();
        foreach ($orgs as $item) {
            if (empty($item->jcbid)) {
                continue;
            }
            for ($page = 0; $page < 999; $page++) {
                $result = \College::StudentsJx($item->jcbid, $page);
                if ($result) {
                    foreach ($result as $items) {
                        if (!is_numeric($items->xh)) {
                            continue;
                        }
                        $student = Student::query()->where(['u_code' => $items->xh])->first();
                        if (!$student) {
                            $student = new Student();
                        }
                        $student->u_name = $items->xm;
                        $student->u_code = $items->xh;
                        $student->nick_name = '';
                        $student->head = '';
                        $student->wx_key = '';
                        $class = mb_substr($item->organizational_name, 0, 2, 'utf-8');
                        if ($class == '业务') {
                            $student->teach_class = $item->jcbid;
                        } else {
                            $student->business_class = $item->jcbid;
                        }
                        $student->access_token = str_random(60);
                        if (!$student->save()) {
                            throw new \Exception('绑定学生失败');
                        }
                    }

                } else {
                    break;
                }
            };
            for ($page = 0; $page < 999; $page++) {
                $result = \College::StudentsYw($item->jcbid, $page);
                if ($result) {
                    foreach ($result as $items) {
                        if (!is_numeric($items->xh)) {
                            continue;
                        }
                        $student = Student::query()->where(['u_code' => $items->xh])->first();
                        if (!$student) {
                            $student = new Student();
                            $student->nick_name = '';
                            $student->head = '';
                            $student->wx_key = '';
                        }
                        $student->u_name = $items->xm;
                        $student->u_code = $items->xh;

                        $class = mb_substr($item->organizational_name, 0, 2, 'utf-8');
                        if ($class == '业务') {
                            $student->teach_class = $item->jcbid;
                        } else {
                            $student->business_class = $item->jcbid;
                        }
                        $student->access_token = str_random(60);
                        if (!$student->save()) {
                            throw new \Exception('绑定学生失败');
                        }
                    }

                } else {
                    break;
                }
            };

            for ($page = 0; $page < 999; $page++) {
                $teachers = \College::Teachers($item->jcbid, $page);
                if ($teachers) {
                    foreach ($teachers as $items) {
                        $teacher = Teacher::query()->where(['teachers_code' => $items->gh])->first();
                        if (!$teacher) {
                            $teacher = new Teacher();
                        }
                        $teacher->teachers_name = $items->xm;
                        $teacher->teachers_code = $items->gh;
                        $teacher->nick_name = '';
                        $teacher->head = '';
                        $teacher->wx_key = '';
                        $teacher->access_token = str_random(60);
                        $teacher->mobile = $items->sjh;
                        $teacher->org = $item->jcbid;
                        if (!$teacher->save()) {
                           dd($teacher->getErrors());
                        }
                    }
                } else {
                    break;
                };

            }
        }
       // Student::query()->where( ['updated_at', '<', $time])->delete();
        }catch (\Exception $e){
            var_dump($e);
        }
    }
}
